AddCSLuaFile( "cl_init.lua" ) -- Make sure clientside
AddCSLuaFile( "shared.lua" )  -- and shared scripts are sent.
 
include('shared.lua')
 
function ENT:Initialize()
 
	self:SetModel( "models/props_c17/Lockers001a.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_NONE )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetColor( 0, 255, 0, 255 )
	self:SetNWInt("storedhealth", INV_MEDICINECABINETINITIALHEALTH )
	self:SetHealth(INV_MEDICINECABINETHEALTH)
	
	
	-- Physics is never enabled on this...
	local phys = self:GetPhysicsObject()
	if phys:IsValid() then
		phys:EnableMotion(false)
	end
end
 
function ENT:Use( activator, caller )
	local storedhealth = self:GetNWInt("storedhealth")
	
	if(storedhealth >= INV_MEDICINECABINETGIVEAMOUNT) then
		-- Give player health, if they have the capacity for it...
		if(activator:Health() + INV_MEDICINECABINETGIVEAMOUNT <= activator:GetMaxHealth()) then
			self:SetNWInt("storedhealth", storedhealth - INV_MEDICINECABINETGIVEAMOUNT)
			activator:SetHealth(activator:Health() + INV_MEDICINECABINETGIVEAMOUNT)
		end
	end
    return
end
 
function ENT:Think()
	local storedhealth = self:GetNWInt("storedhealth")
	if storedhealth + INV_MEDICINECABINETREFILLAMOUNT <= INV_MEDICINECABINETINITIALHEALTH then
		self:SetNWInt("storedhealth", storedhealth + INV_MEDICINECABINETREFILLAMOUNT )
	end

	
	self.Entity:NextThink( CurTime() + INV_MEDICINECABINETREFILLINTERVAL )
    return true
end

function ENT:OnTakeDamage( dmg )

	self:SetHealth(self:Health() - dmg:GetDamage())
	
	local greenmult = self:Health() / INV_MEDICINECABINETHEALTH
	local redmult = (self:Health() / INV_MEDICINECABINETHEALTH)
	redmult = -redmult + 1
	
	self:SetColor( 255 * redmult, 255 * greenmult, 0, 255 )
	
	if(self:Health() <= 0)then
		self:Remove()
	end
end
